/**
 * 
 */
package com.zito.manager;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;


import com.downlove.common.RequestContext;
import com.downlove.common.Storage.FileInfo;
import com.downlove.common.StorageService;
import com.downlove.database.SequenceManager;
import com.zito.ErrMsgException;
import com.zito.bean.Ads;
import com.zito.common.Paginator;

/**
 * @author YunHai-Home
 * 
 */
public class AdsManager {
	Ads ads = new Ads();
	private Paginator paginator;
	int fileSize = 1024 * 1024;

	public boolean adsActive() throws ErrMsgException {
		RequestContext rc = RequestContext.get();
		long tempid = rc.param("id", -1);
		if (tempid == -1)
			return false;
		ads.setId(tempid);
		return ads.Active();
	}

	public boolean operatingPost() throws ErrMsgException {
		RequestContext rc = RequestContext.get();
		long tempid = rc.param("id", -1);
		return tempid == -1 ? insertAds(rc) : editAds(rc);
	}

	public boolean insertAds(RequestContext rc) throws ErrMsgException {
		ads = null;
		ads = rc.form(Ads.class);
		List<String> error = ads.doValidatorList();
		if (error.size() > 0)
			throw new ErrMsgException(error.get(0));
		ads.setTypeid(1L);
		ads.setId(SequenceManager.nextID(ads.getClass().getSimpleName()));
		// product.setEntid(-1L);
		return ads.Save() != -1L;
	}

	public boolean editAds(RequestContext rc) throws ErrMsgException {
		ads = null;
		ads = rc.form(Ads.class);
		if (ads.getId() == -1)
			return false;
		List<String> error = ads.doValidatorList();
		if (error.size() > 0)
			throw new ErrMsgException(error.get(0));
		return ads.Update();
	}

	public Ads getAds() {
		RequestContext rc = RequestContext.get();
		long id = rc.param("id", -1);
		if (id == -1)
			return null;
		return ads.Get(id);
	}

	public String getPaginator() {
		return this.paginator.getCssPageBlock();
	}

	public FileInfo uploadFile() throws ErrMsgException, IOException {
		RequestContext rc = RequestContext.get();
		Enumeration<String> files = rc.getFileNames();
		FileInfo info = null;
		while (files.hasMoreElements()) {
			String filename = files.nextElement();
			File imgFile = rc.file(filename);
			if (imgFile.length() > fileSize) {
				throw new ErrMsgException("您上传的- " + imgFile.getName()
						+ " -文件过大，系统仅允许上传：" + fileSize / 1024 + "K 大小的文件");
			}
			info = StorageService.MAINPATH.save(imgFile).getFileInfo();
		}
		return info;
	}

	@SuppressWarnings("unchecked")
	public List<Ads> adsList(int pagesize) {
		RequestContext rc = RequestContext.get();
		Ads ads = new Ads();
		int total = ads.TotalCount();
		this.paginator = new Paginator(rc, total, pagesize);
		int totalpages = this.paginator.getTotalPages();
		int curpage = this.paginator.getCurrentPage();
		if (totalpages == 0) {
			curpage = 1;
			totalpages = 1;
		}
		List<Long> tmpList = ads.idList(curpage, pagesize);
		return ads.LoadList(tmpList);
	}

	public boolean deleteAds() throws ErrMsgException {
		RequestContext rc = RequestContext.get();
		Ads ads = new Ads();
		ads = rc.form(Ads.class);
		if (ads.getId() == -1)
			return false;

		return ads.Delete();
	}

	public Ads getPageAds() {
		return ads.Get();
	}
}
